﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Net.Mail;

namespace WinLogicUniversity
{
    class PurchaseManager
    {
        public PurchaseManager() { }
        public PurchaseManager(string s)
        {
            supplierID = s;
        }
        public PurchaseManager(string s, string c)
        {
            supplierID = s;
            clerkID = c;
        }
        public PurchaseManager(List<Object[]> lst)
        {
            list = lst;
        }
        public static string supplierID
        { get; set; }
        public static string clerkID
        { get; set; }
        public static List<Object[]> list
        { get; set; }
        public static List<PurchaseObj> getPurchaseList()
        {
            List<PurchaseObj> plist = PurchaseList.getPurchaseList();
            return plist;
        }
        public static IQueryable getAllSupplier()
        {
            var slist =DBBroker.PurchaseOrderSend.getAllSupplier();
            return slist;
        }
        public static double getTotal()
        {
            double total =DBBroker.PurchaseOrderSend.getTotal();
            return total;
        }
        
        public static IQueryable getPurchaseOrderList()
        {
            var polist =DBBroker.PurchaseOrderSend.getPurchaseList();
            return polist;
        }
        public static double getTotalAmount()
        {
            double total =PurchaseList.getTotal();
            return total;
        }

        public static void sendEmail(string s, string c)
        {
            string sender = "";
            string getter = "";
            DBBroker.PurchaseOrderSend po = new DBBroker.PurchaseOrderSend(supplierID, clerkID);
            sender = DBBroker.PurchaseOrderSend.getSenderEmail();
            getter =DBBroker.PurchaseOrderSend.getSupplierEmail();
            DataTable dt = ConvertListToDataTable(list);
            MailMessage m = new MailMessage(sender, getter);
            m.Subject = "Purchase Order";
            m.IsBodyHtml = true;
            //m.Body = "<i>this is my SEXY body!</i>";
            m.Body = GetMessageBody(dt);
            SmtpClient smtpClient = new SmtpClient("lynx.iss.nus.edu.sg");
            smtpClient.Send(m);
            smtpClient.Dispose();

        }

        public static string GetMessageBody(DataTable dt)
        {
            StringBuilder sb = new StringBuilder();
         if (dt != null && dt.Rows.Count > 0)
                {
                    sb.Append("<table border='1'>");
                    // Table Header.
                    sb.Append("<tr><td>No</td><td>Item No</td><td>Description</td><td>Qty</td><td>Price</td><td>Amount</td></tr>");
                    foreach (DataRow row in dt.Rows)
                    {// Get current index 
                        int currentRow = dt.Rows.IndexOf(row);
                        sb.Append("<tr>");
                        // To display correct No, increase the index as initial index is zero.
                        sb.Append("<td>" + (currentRow + 1) + "</td>");
                        foreach (var item in row.ItemArray)
                        {
                            sb.Append("<td>" + item + "</td>");
                        }
                        sb.Append("</tr>");
                    }
                    sb.Append("</table>");
                }
                // Return 'No dat' if StringBuilder is Null or Empty. Else return the content.
                return string.IsNullOrEmpty(sb.ToString()) ? "No data" : sb.ToString();
        }
        public static DataTable ConvertListToDataTable(List<Object[]> list)
        {
            // New table.
            DataTable table = new DataTable();
            // Get max columns.
            int columns = 0;
            foreach (var array in list)
            {
                if (array.Length > columns)
                {
                    columns = array.Length;
                }
            }
            // Add columns.
            for (int i = 0; i < columns; i++)
            {
                table.Columns.Add();
            }

            // Add rows.
            foreach (var array in list)
            { table.Rows.Add(array); }
            return table;
        }
    }
}
